package com.xilada.xldutils.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.support.design.widget.TextInputLayout;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.xilada.xldutils.R;

/**
 *
 * Created by liaoxiang on 16/9/18.
 */
public class SwitchPasswordEditText extends FrameLayout{

    private TextInputLayout tl_password;
    private EditText et_password;
    private TextView action_switch;
    private boolean showPassword = false;

    public SwitchPasswordEditText(Context context) {
        super(context);
        init(context,null);
    }

    public SwitchPasswordEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public SwitchPasswordEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SwitchPasswordEditText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context,attrs);
    }

    void init(Context context,AttributeSet attrs){
        LayoutInflater.from(context).inflate(R.layout.view_switch_password_edit_text,this,true);

        tl_password = (TextInputLayout) findViewById(R.id.tl_password);
        et_password = (EditText) findViewById(R.id.et_password);
        action_switch = (TextView) findViewById(R.id.action_switch);
        if (attrs!=null){
            TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.SwitchPasswordEditText);
            String hint = a.getString(R.styleable.SwitchPasswordEditText_android_hint);
            if (!TextUtils.isEmpty(hint)){
                tl_password.setHint(hint);
            }
            int length = a.getInt(R.styleable.SwitchPasswordEditText_android_maxLength,18);
            if (length>0){
                et_password.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
            }
            showPassword = a.getBoolean(R.styleable.SwitchPasswordEditText_showPassword,false);
            showPassword(showPassword);
            a.recycle();
        }
        action_switch.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                showPassword(!showPassword);
            }
        });
    }

    public void setError(boolean errorEnable,CharSequence charSequence){
        tl_password.setErrorEnabled(errorEnable);
        tl_password.setError(charSequence);
    }

    public void addTextWatcher(TextWatcher textWatcher){
        et_password.addTextChangedListener(textWatcher);
    }

    public void showPassword(boolean show){
        if (showPassword != show) {
            showPassword = show;

            final int selection = et_password.getSelectionEnd();
            action_switch.setSelected(!show);
            if (!show) {
                et_password.setTransformationMethod(null);
            } else {
                et_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
            // And restore the cursor position
            et_password.setSelection(selection);

        }
    }
}
